package com.ddwl.wallet.dao;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.wallet.dao.mapper.UserWalletMapper;
import com.ddwl.wallet.dao.model.UserWallet;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


/**
 * 用户钱包
 */
@Repository
public class UserWalletDao extends TkDao<UserWalletMapper, UserWallet> {


    /**
     * 获取用户钱包
     * @return
     */
    public UserWallet getUserWallet(String userId){
        UserWallet wallet = new UserWallet();
        wallet.setUserId(userId);
        wallet.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        List<UserWallet> userWallets =  baseMapper.select(wallet);
        if(CollectionUtils.isEmpty(userWallets)){
            return createUserWallet(userId);
        }
        return userWallets.get(0);
    }

    public UserWallet getExistWallet(String userId){
        UserWallet wallet = new UserWallet();
        wallet.setUserId(userId);
        wallet.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        List<UserWallet> userWallets =  baseMapper.select(wallet);
        if(!CollectionUtils.isEmpty(userWallets)){
            return userWallets.get(0);
        }
        return null;
    }


    public UserWallet createUserWallet(String userId){
        UserWallet userWallet = UserWallet.createUserWallet(userId);
        insert(userWallet);
        return userWallet;
    }

    public List<UserWallet> queryUserWalletList(List<String> userIds){
        Example example = getExample();
        example.and().andIn("userId",userIds);
        return baseMapper.selectByExample(example);
    }

    public void copyInsert(UserWallet userWallet) {
        baseMapper.insert(userWallet);
    }
}
